from PIL import Image, ImageTk
import matplotlib
from matplotlib import pyplot as plt
matplotlib.use('Agg')

font = {
    'family':'SimHei',
    'weight':'bold',
    'size':12
}
matplotlib.rc("font", **font)

class Chara:
    """
    Chara 角色的父类模板
    """

    def __init__(self):
        self.errorRate = 0  # 容错率
        self.name = "UnknownName"  # 名称
        self.info = "人物未加载 代码错误！"
        self.multiplyingPower = 0  # 倍率
        self.imagePath = "xxx.png"  # 图像名称
        self.rootImagePath = ...
    def get_charts(self,size=None):
        """
        输出能力图

        @return:
        """
        global CharaInfo
        if size is None:
            size = (300,300)
        plt.bar(["得分倍率", "容错个数"], [self.multiplyingPower, self.errorRate])

        plt.savefig("./Tmp/chart.png")
        plt.clf()

        image = Image.open("./Tmp/chart.png")
        CharaInfo = ImageTk.PhotoImage(image.resize((int(size[0]), int(size[1]))))

        return CharaInfo


    def reLoad(self):
        self.basic_path = r"./window_source/Chara/"  # 图片根目录路径

        self.rootImagePath = self.basic_path + self.imagePath  # 图片根目录路径
        self.headImagePath = self.basic_path + "head_image/" + self.name + ".png" # 头像目录
        self.infofile = self.basic_path + f"/info/{self.name}.txt"
        with open(self.infofile, 'r', encoding='utf-8') as f:
            self.info = f.read()

    def get_tkinter_image(self, resize=None, rate_size=None, imageType=None):
        """
        获取 Tkinter 上的 mytk

        请注意给她一个全局的作用域(¬_¬)

        @param resize: 修改大小(w,h)
        @param rate_size: 等比例缩放(w,h)
        @param imageType: 没有设置imageType的时候返回大图否则就是头像
        @return:
        """
        global CharaInfo # ImageTk典型Bug 需要设置全局
        image = Image.open(self.rootImagePath if imageType is None else self.headImagePath)
        CharaInfo = ImageTk.PhotoImage(image)
        if rate_size is not None:
            CharaInfo = ImageTk.PhotoImage(
                image.resize((int(image.size[0] * rate_size), int(image.size[1] * rate_size)))
            )
        elif resize is not None:
            CharaInfo = ImageTk.PhotoImage(
                image.resize((int(resize[0]), int(resize[1])))
            )
        else:
            CharaInfo = ImageTk.PhotoImage(image)

        return CharaInfo

    def __str__(self):
        return self.name

